package com.sljy.hzk.ui.detection;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.tkk.api.RxEventProcessor;
import com.unnamed.b.atv.model.TreeNode;


/**
 * Created by Bogdan Melnychuk on 2/15/15.
 */
public class SelectableItemHolder extends TreeNode.BaseNodeViewHolder<SelectableItemHolder.Item> {
    private TextView tvValue;
    private CheckBox checkBox;
    //层级
    private int level;

    public SelectableItemHolder(Context context,int level) {
        super(context);
        this.level = level;
    }

    @Override
    public View createNodeView(final TreeNode node, SelectableItemHolder.Item value) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.layout_icon_node, null, false);
        view.setPadding((level - 1)*60 + 40,9,0,9);
        tvValue = (TextView) view.findViewById(R.id.node_value);
        tvValue.setText(Html.fromHtml(value.getpName()));
        checkBox =  view.findViewById(R.id.check_box);
        checkBox.setChecked(node.isSelected());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                node.setSelected(isChecked);
                if (isChecked){
                    RxEventProcessor.get().post("isChecked", node);
                }
            }
        });
        return view;
    }


    @Override
    public void toggleSelectionMode(boolean editModeEnabled) {
        checkBox.setVisibility(editModeEnabled ? View.VISIBLE : View.GONE);
        checkBox.setChecked(mNode.isSelected());
    }
    public static class Item{
        private String pName;
        private int pId;
        private String allName;

        public Item(String pName, int pId, String allName) {
            this.pName = pName;
            this.pId = pId;
            this.allName = allName;
        }

        public String getAllName() {
            return allName;
        }

        public void setAllName(String allName) {
            this.allName = allName;
        }

        public Item(String pName, int pId) {
            this.pName = pName;
            this.pId = pId;
        }

        public String getpName() {
            return pName;
        }

        public void setpName(String pName) {
            this.pName = pName;
        }

        public int getpId() {
            return pId;
        }

        public void setpId(int pId) {
            this.pId = pId;
        }
    }
}
